﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using Microsoft.SharePoint;
using System.Linq;

namespace SP_XHWeb.WebParts.EmployeePhoneInfo
{
    public partial class EmployeePhoneInfoUserControl : UserControl
    {
        public string EmployeeInfoUrl = "/_layouts/SP_XHWeb/EmployeePhoneDetails.aspx?item_id=";
        protected void Page_Load(object sender, EventArgs e)
        {
            string hostUrl = this.Page.Request.Url.Scheme + "://" + this.Page.Request.Url.Authority;
            EmployeeInfoUrl = hostUrl + EmployeeInfoUrl;
            if (!IsPostBack)
            {
                ddl1.DataSource = InitCompany();
                ddl1.DataBind();
                gvEp.DataSource = GetEmployee();
                gvEp.DataBind();

                ddl2.DataSource = InitDepartment();
                ddl2.DataBind();
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string strComp = ddl1.SelectedItem.Text.Trim();
            string strDept = ddl2.SelectedItem.Text.Trim();
            string strName = txt1.Text.Trim();
            if (strComp == "选择公司" && strDept == "选择部门" && string.IsNullOrEmpty(strName))
            {
                List<EmployeePhoneItem> listes = GetEmployee();
                gvEp.DataSource = listes;
                if (listes != null && listes.Count > 0)
                {
                    gvEp.PageIndex = 0;
                }
                gvEp.DataBind();
                
            }
            else
            {
                List<EmployeePhoneItem> lists = GetEmployee(strComp, strDept, strName);
                if (lists != null && lists.Count != 0)
                {
                    gvEp.DataSource = lists;
                    if (lists != null && lists.Count > 0)
                    {
                        gvEp.PageIndex = 0;
                    }
                    gvEp.DataBind();
                    
                }
                else
                {
                    gvEp.DataSource = null;
                    gvEp.DataBind();
                }
            }
        }

        /// <summary>
        /// 查看所有员工信息
        /// </summary>
        /// <returns></returns>
        private List<EmployeePhoneItem> GetEmployee()
        {
            List<EmployeePhoneItem> lists = new List<EmployeePhoneItem>();
            string hostUrl = this.Page.Request.Url.Scheme + "://" + this.Page.Request.Url.Authority;
            //string siteUrl = this.Page.Request.Url.ToString().ToLower();
            if (!string.IsNullOrEmpty(hostUrl))
            {
                using (SPSite site = new SPSite(hostUrl + "/companyinfo/default.aspx"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList list = web.Lists.TryGetList("EmployeeInfo");
                        if (list != null && list.ItemCount != 0)
                        {
                            foreach (SPListItem item in list.Items)
                            {
                                lists.Add(new EmployeePhoneItem(item.ID, item["姓名"].ToString().Trim(), item["公司"].ToString().Trim(), item["部门"].ToString().Trim(), item["移动电话"].ToString().Trim(), item["排序数"] != null && !string.IsNullOrEmpty(item["排序数"].ToString()) ? Int32.Parse(item["排序数"].ToString()) : 9999));
                            }
                        }
                    }
                }
            }
            if (lists != null && lists.Count > 0)
            {
                lists = (from emp in lists orderby emp.OrderNum ascending select emp).ToList<EmployeePhoneItem>();
            }
            return lists;
        }

        /// <summary>
        /// 查询员工信息
        /// </summary>
        /// <param name="strComp">选择公司</param>
        /// <param name="strDept">选择部门</param>
        /// <param name="strName">姓名</param>
        /// <returns></returns>
        private List<EmployeePhoneItem> GetEmployee(string strComp, string strDept, string strName)
        {
            List<EmployeePhoneItem> lists = new List<EmployeePhoneItem>();
            string hostUrl = this.Page.Request.Url.Scheme + "://" + this.Page.Request.Url.Authority;

            if (!string.IsNullOrEmpty(hostUrl))
            {
                using (SPSite site = new SPSite(hostUrl + "/companyinfo/default.aspx"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList list = web.Lists.TryGetList("EmployeeInfo");
                        if (list != null && list.ItemCount != 0)
                        {
                            //公司不为空
                            if (strComp != "选择公司" && strDept == "选择部门" && string.IsNullOrEmpty(strName))
                            {
                                foreach (SPListItem item in list.Items)
                                {
                                    if (item["公司"].ToString().Trim().ToLower() == strComp.Trim().ToLower())
                                    {
                                        lists.Add(new EmployeePhoneItem(item.ID, item["姓名"].ToString().Trim(), item["公司"].ToString().Trim(), item["部门"].ToString().Trim(), item["移动电话"].ToString().Trim(), item["排序数"] != null && !string.IsNullOrEmpty(item["排序数"].ToString()) ? Int32.Parse(item["排序数"].ToString()) : 9999));
                                    }
                                }
                            }
                            //部门不为空
                            else if (strComp == "选择公司" && strDept != "选择部门" && string.IsNullOrEmpty(strName))
                            {
                                foreach (SPListItem item in list.Items)
                                {
                                    if (item["部门"].ToString().Trim().ToLower() == strDept.Trim().ToLower())
                                    {
                                        lists.Add(new EmployeePhoneItem(item.ID, item["姓名"].ToString().Trim(), item["公司"].ToString().Trim(), item["部门"].ToString().Trim(), item["移动电话"].ToString().Trim(), item["排序数"] != null && !string.IsNullOrEmpty(item["排序数"].ToString()) ? Int32.Parse(item["排序数"].ToString()) : 9999));
                                    }
                                }
                            }
                            //姓名不为空
                            else if (strComp == "选择公司" && strDept == "选择部门" && !string.IsNullOrEmpty(strName))
                            {
                                foreach (SPListItem item in list.Items)
                                {
                                    if (item["姓名"].ToString().Trim().ToLower().Contains(strName.Trim().ToLower()))
                                    {
                                        lists.Add(new EmployeePhoneItem(item.ID, item["姓名"].ToString().Trim(), item["公司"].ToString().Trim(), item["部门"].ToString().Trim(), item["移动电话"].ToString().Trim(), item["排序数"] != null && !string.IsNullOrEmpty(item["排序数"].ToString()) ? Int32.Parse(item["排序数"].ToString()) : 9999));
                                    }
                                }
                            }
                            //公司与部门不为空
                            else if (strComp != "选择公司" && strDept != "选择部门" && string.IsNullOrEmpty(strName))
                            {
                                foreach (SPListItem item in list.Items)
                                {
                                    if (item["公司"].ToString().Trim().ToLower() == strComp.Trim().ToLower() && item["部门"].ToString().Trim().ToLower() == strDept.Trim().ToLower())
                                    {
                                        lists.Add(new EmployeePhoneItem(item.ID, item["姓名"].ToString().Trim(), item["公司"].ToString().Trim(), item["部门"].ToString().Trim(), item["移动电话"].ToString().Trim(), item["排序数"] != null && !string.IsNullOrEmpty(item["排序数"].ToString()) ? Int32.Parse(item["排序数"].ToString()) : 9999));
                                    }
                                }
                            }
                            //公司与姓名不为空
                            else if (strComp != "选择公司" && strDept == "选择部门" && !string.IsNullOrEmpty(strName))
                            {
                                foreach (SPListItem item in list.Items)
                                {
                                    if (item["公司"].ToString().Trim().ToLower() == strComp.Trim().ToLower() && item["姓名"].ToString().Trim().ToLower().Contains(strName.Trim().ToLower()))
                                    {
                                        lists.Add(new EmployeePhoneItem(item.ID, item["姓名"].ToString().Trim(), item["公司"].ToString().Trim(), item["部门"].ToString().Trim(), item["移动电话"].ToString().Trim(), item["排序数"] != null && !string.IsNullOrEmpty(item["排序数"].ToString()) ? Int32.Parse(item["排序数"].ToString()) : 9999));
                                    }
                                }
                            }
                            //部门与姓名不为空
                            else if (strComp == "选择公司" && strDept != "选择部门" && !string.IsNullOrEmpty(strName))
                            {
                                foreach (SPListItem item in list.Items)
                                {
                                    if (item["部门"].ToString().Trim().ToLower() == strDept.Trim().ToLower() && item["姓名"].ToString().Trim().ToLower().Contains(strName.Trim().ToLower()))
                                    {
                                        lists.Add(new EmployeePhoneItem(item.ID, item["姓名"].ToString().Trim(), item["公司"].ToString().Trim(), item["部门"].ToString().Trim(), item["移动电话"].ToString().Trim(), item["排序数"] != null && !string.IsNullOrEmpty(item["排序数"].ToString()) ? Int32.Parse(item["排序数"].ToString()) : 9999));
                                    }
                                }
                            }
                            //全不为空
                            else if (strComp != "选择公司" && strDept != "选择部门" && !string.IsNullOrEmpty(strName))
                            {
                                foreach (SPListItem item in list.Items)
                                {
                                    if (item["公司"].ToString().Trim().ToLower() == strComp.Trim().ToLower() && item["部门"].ToString().Trim().ToLower() == strDept.Trim().ToLower() && item["姓名"].ToString().Trim().ToLower().Contains(strName.Trim().ToLower()))
                                    {
                                        lists.Add(new EmployeePhoneItem(item.ID, item["姓名"].ToString().Trim(), item["公司"].ToString().Trim(), item["部门"].ToString().Trim(), item["移动电话"].ToString().Trim(), item["排序数"] != null && !string.IsNullOrEmpty(item["排序数"].ToString()) ? Int32.Parse(item["排序数"].ToString()) : 9999));
                                    }
                                }
                            }
                            //全为空
                            else
                            {
                                return GetEmployee();
                            }

                        }
                    }
                }
            }
            if (lists != null && lists.Count > 0)
            {
                lists = (from emp in lists orderby emp.OrderNum ascending select emp).ToList<EmployeePhoneItem>();
            }
            return lists;
        }

        //防止GridView翻页时报错
        protected void gvEp_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvEp.PageIndex = e.NewPageIndex;
            string strComp = ddl1.SelectedItem.Text.Trim();
            string strDept = ddl2.SelectedItem.Text.Trim();
            string strName = txt1.Text.Trim();
            gvEp.DataSource = GetEmployee(strComp, strDept, strName);
            gvEp.DataBind();

        }



        //通讯录查询
        protected void imb1_Click(object sender, ImageClickEventArgs e)
        {
            string strComp = ddl1.SelectedItem.Text.Trim();
            string strDept = ddl2.SelectedItem.Text.Trim();
            string strName = txt1.Text.Trim();
            if (strComp == "选择公司" && strDept == "选择部门" && string.IsNullOrEmpty(strName))
            {
                gvEp.DataSource = GetEmployee();
                gvEp.DataBind();
            }
            else
            {
                List<EmployeePhoneItem> lists = GetEmployee(strComp, strDept, strName);
                if (lists != null && lists.Count != 0)
                {
                    gvEp.DataSource = lists;
                    gvEp.DataBind();
                }
                else
                {
                    gvEp.DataSource = null;
                    gvEp.DataBind();
                }
            }
        }



        private List<string> InitCompany()
        {
            List<string> comps = new List<string>();
            comps.Add("选择公司");
            string hostUrl = this.Page.Request.Url.Scheme + "://" + this.Page.Request.Url.Authority;
            if (!string.IsNullOrEmpty(hostUrl))
            {
                using (SPSite site = new SPSite(hostUrl + "/companyinfo"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList list = web.Lists.TryGetList("EmployeeInfo");
                        if (list != null && list.ItemCount != 0)
                        {
                            foreach (SPListItem item in list.Items)
                            {
                                if (!comps.Contains(item["公司"].ToString().Trim()))
                                {
                                    comps.Add(item["公司"].ToString().Trim());
                                }
                            }
                        }
                    }
                }
            }
            return comps;
        }

        private List<string> InitDepartment()
        {
            List<string> depts = new List<string>();
            depts.Add("选择部门");
            string hostUrl = this.Page.Request.Url.Scheme + "://" + this.Page.Request.Url.Authority;
            if (!string.IsNullOrEmpty(hostUrl))
            {
                using (SPSite site = new SPSite(hostUrl + "/companyinfo"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList list = web.Lists.TryGetList("EmployeeInfo");
                        if (list != null && list.ItemCount != 0)
                        {
                            foreach (SPListItem item in list.Items)
                            {
                                if (!depts.Contains(item["部门"].ToString().Trim()))
                                {
                                    depts.Add(item["部门"].ToString().Trim());
                                }
                            }
                        }
                    }
                }
            }
            return depts;
        }
    }

    public class EmployeePhoneItem
    {
        public int EID { get; set; }

        public string Name { get; set; }

        public string Company { get; set; }

        public string Department { get; set; }

        public string Telephone { get; set; }

        public int OrderNum { get; set; }

        public EmployeePhoneItem(int eid, string name, string company, string department, string telephone, int orderNum)
        {
            this.EID = eid;
            this.Name = name;
            this.Company = company;
            this.Department = department;
            this.Telephone = telephone;
            this.OrderNum = orderNum;
        }
    }


}
